package com.zq.assistant.utils;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Locale;

public class FileUtils {


    public static void deleteFile(String filePath) {

        File file = new File(filePath);

        if (!file.exists() || !file.isDirectory())
            return;

        if (file.isFile()) {

            file.delete();
        } else if (file.isDirectory()) {

            File[] childFiles = file.listFiles();

            for (File childFile : childFiles) {

                deleteFile(childFile.getAbsolutePath());
            }
        }
    }


    public static boolean copyFile(File rawFile, File destFile) {

        if (rawFile == null || !rawFile.exists()) {
            return false;
        }

        try {
            InputStream inputStream = new FileInputStream(rawFile);
            FileOutputStream fos = new FileOutputStream(destFile);

            int len;
            byte[] buffer = new byte[1024];

            while ((len = inputStream.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }

            inputStream.close();
            fos.flush();
            fos.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 压缩上传的图片文件
     * 宽高限制1024x1024 文件大小1MB
     *
     * @param imageFile
     * @return
     */
    public static File getUploadImageFile(File imageFile) {

        return ImageCompress.compressImageFile(imageFile, 1024, 1024, 1024 * 1024);
    }


    /**
     * 判断sd卡是否可用
     *
     * @return
     */
    public static boolean isSdCardAvailable() {

        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }



    public static String readStringFromFile(File file) {

        if (file == null || !file.exists()) {
            return null;
        }

        try {
            FileReader fileReader = new FileReader(file);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            StringWriter stringWriter = new StringWriter();

            String line;
            while ((line = bufferedReader.readLine()) != null) {

                stringWriter.append(line);
            }

            bufferedReader.close();
            stringWriter.flush();
            stringWriter.close();
            return stringWriter.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean writeStringToFile(String string, File file) {

        if (file == null) {
            return false;
        }

        try {
            FileWriter fileWriter = new FileWriter(file);

            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            StringReader stringReader = new StringReader(string);

            char[] buffer = new char[1024 * 4];
            int len;
            while ((len = stringReader.read(buffer)) != -1) {

                bufferedWriter.write(buffer, 0, len);
            }

            stringReader.close();
            bufferedWriter.flush();
            bufferedWriter.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public enum Type{

        /**
         * JEPG.
         */
        JPEG("FFD8FF"),

        /**
         * PNG.
         */
        PNG("89504E47"),

        /**
         * GIF.
         */
        GIF("47494638"),

        /**
         * TIFF.
         */
        TIFF("49492A00"),

        /**
         * Windows Bitmap.
         */
        BMP("424D"),

        /**
         * CAD.
         */
        DWG("41433130"),

        /**
         * Adobe Photoshop.
         */
        PSD("38425053"),

        /**
         * Rich Text Format.
         */
        RTF("7B5C727466"),

        /**
         * XML.
         */
        XML("3C3F786D6C"),

        /**
         * HTML.
         */
        HTML("68746D6C3E"),

        /**
         * Email [thorough only].
         */
        EML("44656C69766572792D646174653A"),

        /**
         * Outlook Express.
         */
        DBX("CFAD12FEC5FD746F"),

        /**
         * Outlook (pst).
         */
        PST("2142444E"),

        /**
         * MS Word/Excel.
         */
        XLS_DOC("D0CF11E0"),

        /**
         * MS Access.
         */
        MDB("5374616E64617264204A"),

        /**
         * WordPerfect.
         */
        WPD("FF575043"),

        /**
         * Postscript.
         */
        EPS("252150532D41646F6265"),

        /**
         * Adobe Acrobat.
         */
        PDF("255044462D312E"),

        /**
         * Quicken.
         */
        QDF("AC9EBD8F"),

        /**
         * Windows Password.
         */
        PWL("E3828596"),

        /**
         * ZIP Archive.
         */
        ZIP("504B0304"),

        /**
         * RAR Archive.
         */
        RAR("52617221"),

        /**
         * Wave.
         */
        WAV("57415645"),

        /**
         * AVI.
         */
        AVI("41564920"),

        /**
         * Real Audio.
         */
        RAM("2E7261FD"),

        /**
         * Real Media.
         */
        RM("2E524D46"),

        /**
         * MPEG (mpg).
         */
        MPG("000001BA"),

        /**
         * Quicktime.
         */
        MOV("6D6F6F76"),

        /**
         * Windows Media.
         */
        ASF("3026B2758E66CF11"),

        /**
         * MIDI.
         */
        MID("4D546864");

        private String value = "";

        /**
         * Constructor.
         */
        private Type(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    private static String bytesToHexString(byte[] src){

        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte aSrc : src) {
            int v = aSrc & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    private static String getFileContent(String filePath) {

        byte[] b = new byte[28];

        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream(filePath);
            inputStream.read(b, 0, 28);
            return bytesToHexString(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static Type getType(String filePath) {

        String fileHead = getFileContent(filePath);

        if (fileHead == null || fileHead.length() == 0) {
            return null;
        }

        fileHead = fileHead.toUpperCase(Locale.CHINA);

        Type[] fileTypes = Type.values();

        for (Type type : fileTypes) {
            if (fileHead.startsWith(type.getValue())) {
                return type;
            }
        }
        return null;
    }
}
